package com.mwt.oes.controller;

import com.mwt.oes.domain.Question;
import com.mwt.oes.service.QuestionService;
import com.mwt.oes.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author caiwl
 * @date 2023/10/25 21:14
 */
@CrossOrigin
@RestController
@RequestMapping("/api/que")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @RequestMapping("/list")
    public ServerResponse list(@RequestParam(name = "singleContent", defaultValue = "") String singleContent) {
        List<Question> questions = questionService.selectList(singleContent);
        return ServerResponse.createBySuccess("获取题目列表成功", questions);
    }

    @RequestMapping("/insert")
    public ServerResponse insertOne(@RequestBody Question question) {
        int result = questionService.insertOne(question);
        if(result > 0) {
            return ServerResponse.createBySuccess("新增成功",null);
        } else {
            return ServerResponse.createByError("数据库错误，新增失败");
        }
    }

    @RequestMapping("/update")
    public ServerResponse updateOne(@RequestBody Question question) {
        int result = questionService.updateOne(question);
        if(result > 0) {
            return ServerResponse.createBySuccess("更新成功",null);
        } else {
            return ServerResponse.createByError("数据库错误，更新失败");
        }
    }

    @RequestMapping(value = "/deleteByQueId/{queId}")
    public ServerResponse deleteByQueId(@PathVariable Integer queId){
        int result = questionService.deleteByQueId(queId);
        if(result > 0) {
            return ServerResponse.createBySuccess("删除成功",null);
        } else {
            return ServerResponse.createByError("数据库错误，删除失败");
        }
    }

}
